package com.blustreaksw.cyclecoach.test;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blustreaksw.cyclecoach.R;
import com.blustreaksw.cyclecoach.data.UserProfile;
import com.blustreaksw.cyclecoach.data.UserProfile.Gender;
import com.blustreaksw.cyclecoach.data.UserProfile.MeasurementUnits;

public class UserDatabaseTestActivity extends Activity {
	
	private static final int ARRAY_SIZE = 4;
	private static final String[] tUserNames = { "tom", "alicia", "callie", "frankie" };
	private static final String[] tUserEmail = { "@gmail", "@yahoo", "@msn", "@aol" };
	private static final String[] tDeviceName = { "Wahoo", "Polar", "viiiiv", "Garmin" };
	
	
	Button mInsertNew;
	Button mGetList;
	Button mUpdateUser;
	Button mGetUser;
	Button mRandomizeProfileValues;
	Button mDeleteUser;
	EditText mConsole;
	
	UserProfile up;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_user_database_activity);
		
		mInsertNew = (Button)findViewById(R.id.test_userDbActivity_insertNew);
		mInsertNew.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				insertProfileValues();
			}
		});
		
		mGetList = (Button)findViewById(R.id.test_userDbActivity_getList);
		mGetList.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				printUserList();
			}
		});
		mUpdateUser = (Button)findViewById(R.id.test_userDbActivity_updateUser);
		mUpdateUser.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				updateRandomUser();
			}
		});
		
		mGetUser = (Button)findViewById(R.id.test_userDbActivity_getUser);
		mGetUser.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				loadRandomProfileValuesFromDb();
			}
		});
		
		mRandomizeProfileValues = (Button)findViewById(R.id.test_userDbActivity_randomize);
		mRandomizeProfileValues.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				setProfileValuesRandom();
			}
		});
		
		mDeleteUser = (Button)findViewById(R.id.test_userDbActivity_deleteUser);
		mDeleteUser.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				up.deleteUser(up.getId());
			}
		});
		
		mConsole = (EditText)findViewById(R.id.test_userDbActivity_console);
		
		up = UserProfile.getInstance(getApplicationContext());
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(100);
		list.add(120);
		list.add(140);
		list.add(160);
		list.add(180);
		up.configureCyclingHRZones(list);
		up.configureRunningHRZones(list);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	
	
	public String userProfileToString() {
		StringBuilder output = new StringBuilder();
		
		output.append("*********\n");
		output.append("User: " + up.getUserName() + "\n");
		output.append("ID: " + up.getId() + "\n");
		output.append("Units: " + up.getDisplayUnits().toString() + "\n");
		output.append("Email: " + up.getUserEmail() + "\n");
		if(up.getDisplayUnits() == MeasurementUnits.METRIC){
			output.append("Weight: " + up.getWeight() + " kg (" + up.getWeightVal() + ")\n");
			output.append("Height: " + up.getHeight() + " cm (" + up.getHeightVal() + ")\n");
		} else {
			output.append("Weight: " + up.getWeight() + " lbs (" + up.getWeightVal() + ")\n");
			output.append("Height: " + up.getHeight() + " in (" + up.getHeightVal() + ")\n");
		}
		
		output.append("Age: " + up.getAge() + "\n");
		output.append("DOB: " + up.getDateOfBirthString() + "\n");
		output.append("RestHR: " + up.getRestingHeartRate() + "\n");
		output.append("FTHR: " + up.getFunctionalThresholdHeartRate() + "\n");
		output.append("FTP: " + up.getFunctionalThresholdPower() + "\n");
		output.append("Gender: " + up.getGender().toString() + "\n");
		output.append("HRM Name: " + up.getHeartRateMonitorName() + "\n");
		output.append("HRM Address: " + up.getHeartRateMonitorAddress() + "\n");
		output.append("CSC Name: " + up.getCyclingSpeedCadenceSensorName() + "\n");
		output.append("CSC Address: " + up.getCyclingSpeedCadenceSensorAddress() + "\n");
		output.append("POW Name: " + up.getCyclingPowerMeterName() + "\n");
		output.append("POW Address: " + up.getCyclingPowerMeterAddress() + "\n");
		output.append("WheelCircumference: " + up.getCycleWheelCircumference() + "\n");
		output.append("*********\n");
		
		// TODO - running/cycling zones getters
		
		return output.toString();
	}
	
	public void setProfileValuesRandom() {
		up.setUserName(tUserNames[getRandom(ARRAY_SIZE)]);
		up.setUserEmail(tUserEmail[getRandom(ARRAY_SIZE)]);
		up.setWeight(getRandom(20, 200));
		up.setHeight(getRandom(40, 100));
		up.setHeartRateMonitorName(tDeviceName[getRandom(ARRAY_SIZE)]);
		up.setCyclingPowerMeterName(tDeviceName[getRandom(ARRAY_SIZE)]);
		up.setCyclingSpeedCadenceSensorName(tDeviceName[getRandom(ARRAY_SIZE)]);
		up.setHeartRateMonitorAddress(getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9));
		up.setCyclingPowerMeterAddress(getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9));
		up.setCyclingSpeedCadenceSensorAddress(getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9) + ":" +
				getRandom(0, 9) + getRandom(0, 9));
		up.setFunctionalThresholdHeartRate(getRandom(120, 180));
		up.setFunctionalThresholdPower(getRandom(120, 250));
		up.setGender(getRandom(2) == 0 ? Gender.GENDER_MALE : Gender.GENDER_FEMALE);
		up.setDateOfBirth(getRandom(1975, 2010), getRandom(0,12), getRandom(1,27));
		up.setCycleWheelCircumference(Double.parseDouble(String.format("%d.%d", getRandom(1, 3), getRandom(0,100))));
		
		StringBuilder output = new StringBuilder(mConsole.getText());
		output.append(userProfileToString());
		mConsole.setText(output.toString());
	}
	
	public void insertProfileValues() {
		if(!up.insertNewUserFromCurrentProfile(getApplicationContext())) return;
		StringBuilder output = new StringBuilder(mConsole.getText());
		output.append("Added user " + up.getUserName() + "\n");
		mConsole.setText(output.toString());
	}
	
	public void loadRandomProfileValuesFromDb() {
		ArrayList<UserProfile.UserNameAndIdContainer> users = 
				new ArrayList<UserProfile.UserNameAndIdContainer>();
		
		if(!up.getUserList(users)) {
			StringBuilder output = new StringBuilder(mConsole.getText());
			output.append("User Db is empty\n");
			mConsole.setText(output.toString());
			return;
		}
		
		StringBuilder userList = new StringBuilder();
		userList.append("UserList:\n");
		for(int i = 0; i < users.size(); i++) {
			userList.append(String.format("%d\t%s\n", users.get(i).getUserId(), users.get(i).getUserName()));
		}
		
		userList.append("*****\n");
		appendToConsole(userList.toString());
		
		up.loadUserFromDb(getRandom(users.size()));
		StringBuilder output = new StringBuilder(mConsole.getText());
		output.append(userProfileToString());
		mConsole.setText(output.toString());
	}
	
	public void updateRandomUser() {
		ArrayList<UserProfile.UserNameAndIdContainer> users = 
				new ArrayList<UserProfile.UserNameAndIdContainer>();
		
		if(!up.getUserList(users)) {
			appendToConsole("User Db is empty\n");
			return;
		}
		
		up.loadUserFromDb(getRandom(users.size()));
		appendToConsole("Old values: \n" + userProfileToString() + "New values: \n");
		setProfileValuesRandom();
		up.updateUserFromCurrentProfileValues(getApplicationContext());
		up.loadUserFromDb(up.getId());
		appendToConsole("Reloaded values: \n" + userProfileToString());
	}
	
	public void printUserList() {
		ArrayList<UserProfile.UserNameAndIdContainer> users = 
				new ArrayList<UserProfile.UserNameAndIdContainer>();
		
		if(!up.getUserList(users)) {
			appendToConsole("User Db is empty\n");
			return;
		}
		
		StringBuilder userList = new StringBuilder();
		userList.append("UserList:\n");
		for(int i = 0; i < users.size(); i++) {
			userList.append(String.format("%d\t%s\n", users.get(i).getUserId(), users.get(i).getUserName()));
		}
		
		userList.append("*****\n");
		appendToConsole(userList.toString());
	}
	
	public int getRandom(int maxValue) {
		return (int) Math.floor(maxValue * Math.random());
	}
	
	public int getRandom(int minValue, int maxValue) {
		return ((int) Math.floor((maxValue - minValue) * Math.random())) + minValue;
	}
	
	public void appendToConsole(String s) {
		StringBuilder output = new StringBuilder(mConsole.getText());
		output.append(s);
		mConsole.setText(output.toString());
	}
}
